/*
 * SSD1289_TestApp.c
 *
 * Created: 22.01.2014 21:46:50
 *  Author: Siliconsoul
 */


#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#include "serial.h"

#include <SSD1289.h> 
#include <DefaultFonts.h>

#define SSD_RAND(x) (random() % (x + 1))
// Which Font should be used
//extern const uint8_t SmallFont[] PROGMEM;

void setup() {
    ssd1289_init(LANDSCAPE, ASPECT_4x3);
    ssd1289_setFont(SmallFont);
} 

int main(void) {
    
    int buf[318];
    int x, x2;
    int y, y2;
    int r;
    
    setup();
    
    while (1) {
        // Clear the screen and draw the frame
        ssd1289_clrScr();

        ssd1289_setColor(255, 0, 0);
        ssd1289_fillRect(0, 0, 319, 13);
        ssd1289_setColor(64, 64, 64);
        ssd1289_fillRect(0, 226, 319, 239);
        ssd1289_setColor(255, 255, 255);
        ssd1289_setBackColor(255, 0, 0);
        ssd1289_print("*** VAOID ***", CENTER, 1, 0);
        ssd1289_setColor(255, 128, 128);
        ssd1289_setBackColor(64, 64, 64);
        ssd1289_print("AT&T", LEFT, 227, 0);
        ssd1289_print("(C)2014", RIGHT, 227, 0);
        ssd1289_setColor(255,255,0);
        ssd1289_print("Fo Shizzle!", CENTER, 227, 0);

        ssd1289_setColor(0, 0, 255);
        ssd1289_drawRect(0, 14, 319, 225);
        _delay_ms(5000);
        // Draw crosshairs
        ssd1289_setColor(0, 0, 255);
        ssd1289_setBackColor(0, 0, 0);
        ssd1289_drawLine(159, 15, 159, 224);
        ssd1289_drawLine(1, 119, 318, 119);
        for (int i=9; i<310; i+=10)
        ssd1289_drawLine(i, 117, i, 121);
        for (int i=19; i<220; i+=10)
        ssd1289_drawLine(157, i, 161, i);

        // Draw sin-, cos- and tan-lines
        ssd1289_setColor(0,255,255);
        ssd1289_print("Sin", 5, 15, 0);
        for (int i=1; i<318; i++)
        {
            ssd1289_drawPixel(i,119+(sin(((i*1.13)*3.14)/180)*95));
        }
        
        ssd1289_setColor(255,0,0);
        ssd1289_print("Cos", 5, 27, 0);
        for (int i=1; i<318; i++)
        {
            ssd1289_drawPixel(i,119+(cos(((i*1.13)*3.14)/180)*95));
        }

        ssd1289_setColor(255,255,0);
        ssd1289_print("Tan", 5, 39, 0);
        for (int i=1; i<318; i++)
        {
            ssd1289_drawPixel(i,119+(tan(((i*1.13)*3.14)/180)));
        }

        _delay_ms(2000);

        ssd1289_setColor(0,0,0);
        ssd1289_fillRect(1,15,318,224);
        ssd1289_setColor(0, 0, 255);
        ssd1289_setBackColor(0, 0, 0);
        ssd1289_drawLine(159, 15, 159, 224);
        ssd1289_drawLine(1, 119, 318, 119);

        // Draw a moving sinewave
        x=1;
        for (int i=1; i<(318*20); i++)
        {
            x++;
            if (x==319)
            x=1;
            if (i>319)
            {
                if ((x==159)||(buf[x-1]==119))
                ssd1289_setColor(0,0,255);
                else
                ssd1289_setColor(0,0,0);
                ssd1289_drawPixel(x,buf[x-1]);
            }
            ssd1289_setColor(0,255,255);
            y=119+(sin(((i*1.1)*3.14)/180)*(90-(i / 100)));
            ssd1289_drawPixel(x,y);
            buf[x-1]=y;
        }

        _delay_ms(2000);
        
        ssd1289_setColor(0,0,0);
        ssd1289_fillRect(1,15,318,224);

        // Draw some filled rectangles
        for (int i=1; i<6; i++)
        {
            switch (i)
            {
                case 1:
                ssd1289_setColor(255,0,255);
                break;
                case 2:
                ssd1289_setColor(255,0,0);
                break;
                case 3:
                ssd1289_setColor(0,255,0);
                break;
                case 4:
                ssd1289_setColor(0,0,255);
                break;
                case 5:
                ssd1289_setColor(255,255,0);
                break;
            }
            ssd1289_fillRect(70+(i*20), 30+(i*20), 130+(i*20), 90+(i*20));
        }

        _delay_ms(2000);
        
        ssd1289_setColor(0,0,0);
        ssd1289_fillRect(1,15,318,224);

        // Draw some filled, rounded rectangles
        for (int i=1; i<6; i++)
        {
            switch (i)
            {
                case 1:
                ssd1289_setColor(255,0,255);
                break;
                case 2:
                ssd1289_setColor(255,0,0);
                break;
                case 3:
                ssd1289_setColor(0,255,0);
                break;
                case 4:
                ssd1289_setColor(0,0,255);
                break;
                case 5:
                ssd1289_setColor(255,255,0);
                break;
            }
            ssd1289_fillRoundRect(190-(i*20), 30+(i*20), 250-(i*20), 90+(i*20));
        }
        
        _delay_ms(2000);
        
        ssd1289_setColor(0,0,0);
        ssd1289_fillRect(1,15,318,224);

        // Draw some filled circles
        for (int i=1; i<6; i++)
        {
            switch (i)
            {
                case 1:
                ssd1289_setColor(255,0,255);
                break;
                case 2:
                ssd1289_setColor(255,0,0);
                break;
                case 3:
                ssd1289_setColor(0,255,0);
                break;
                case 4:
                ssd1289_setColor(0,0,255);
                break;
                case 5:
                ssd1289_setColor(255,255,0);
                break;
            }
            ssd1289_fillCircle(100+(i*20),60+(i*20), 30);
        }
        
        _delay_ms(2000);
        
        ssd1289_setColor(0,0,0);
        ssd1289_fillRect(1,15,318,224);

        // Draw some lines in a pattern
        ssd1289_setColor (255,0,0);
        for (int i=15; i<224; i+=5)
        {
            ssd1289_drawLine(1, i, (i*1.44)-10, 224);
        }
        ssd1289_setColor (255,0,0);
        for (int i=224; i>15; i-=5)
        {
            ssd1289_drawLine(318, i, (i*1.44)-11, 15);
        }
        ssd1289_setColor (0,255,255);
        for (int i=224; i>15; i-=5)
        {
            ssd1289_drawLine(1, i, 331-(i*1.44), 15);
        }
        ssd1289_setColor (0,255,255);
        for (int i=15; i<224; i+=5)
        {
            ssd1289_drawLine(318, i, 330-(i*1.44), 224);
        }
        
        _delay_ms(2000);
        
        ssd1289_setColor(0,0,0);
        ssd1289_fillRect(1,15,318,225);

        // Draw some SSD_RAND circles
        for (int i=0; i<100; i++)
        {
            ssd1289_setColor(SSD_RAND(255), SSD_RAND(255), SSD_RAND(255));
            x=32+SSD_RAND(256);
            y=45+SSD_RAND(146);
            r=SSD_RAND(30);
            ssd1289_drawCircle(x, y, r);
        }

        _delay_ms(2000);
        
        ssd1289_setColor(0,0,0);
        ssd1289_fillRect(1,15,318,224);

        // Draw some SSD_RAND rectangles
        for (int i=0; i<100; i++)
        {
            ssd1289_setColor(SSD_RAND(255), SSD_RAND(255), SSD_RAND(255));
            x=2+SSD_RAND(316);
            y=16+SSD_RAND(207);
            x2=2+SSD_RAND(316);
            y2=16+SSD_RAND(207);
            ssd1289_drawRect(x, y, x2, y2);
        }

        _delay_ms(2000);
        
        ssd1289_setColor(0,0,0);
        ssd1289_fillRect(1,15,318,224);

        // Draw some SSD_RAND rounded rectangles
        for (int i=0; i<100; i++)
        {
            ssd1289_setColor(SSD_RAND(255), SSD_RAND(255), SSD_RAND(255));
            x=2+SSD_RAND(316);
            y=16+SSD_RAND(207);
            x2=2+SSD_RAND(316);
            y2=16+SSD_RAND(207);
            ssd1289_drawRoundRect(x, y, x2, y2);
        }

        _delay_ms(2000);
        
        ssd1289_setColor(0,0,0);
        ssd1289_fillRect(1,15,318,224);

        for (int i=0; i<100; i++)
        {
            ssd1289_setColor(SSD_RAND(255), SSD_RAND(255), SSD_RAND(255));
            x=2+SSD_RAND(316);
            y=16+SSD_RAND(209);
            x2=2+SSD_RAND(316);
            y2=16+SSD_RAND(209);
            ssd1289_drawLine(x, y, x2, y2);
        }

        _delay_ms(2000);
        
        ssd1289_setColor(0,0,0);
        ssd1289_fillRect(1,15,318,224);

        for (int i=0; i<10000; i++)
        {
            ssd1289_setColor(SSD_RAND(), SSD_RAND(255), SSD_RAND(255));
            ssd1289_drawPixel(2+SSD_RAND(316), 16+SSD_RAND(209));
        }

        _delay_ms(2000);

        ssd1289_fillScr(0, 0, 255);
        ssd1289_setColor(255, 0, 0);
        ssd1289_fillRoundRect(80, 70, 239, 169);
        
        ssd1289_setColor(255, 255, 255);
        ssd1289_setBackColor(255, 0, 0);
        ssd1289_print("Ende!", CENTER, 93, 0);
        ssd1289_print("Neustart in", CENTER, 119, 0);
        ssd1289_print("wenigen Sekunden...", CENTER, 132, 0);
        //ssd1289_print("That's it!", CENTER, 93, 0);
        //ssd1289_print("Restarting in a", CENTER, 119, 0);
        //ssd1289_print("few seconds...", CENTER, 132, 0);
        
        ssd1289_setColor(0, 255, 0);
        ssd1289_setBackColor(0, 0, 255);
        //ssd1289_print("Runtime: (msecs)", CENTER, 210, 0);
        //ssd1289_printNumI(millis(), CENTER, 225);
        
        _delay_ms (10000);
    }
    
}

void forever_blink(void) {
    
    // Set pin as output
    DDRB  |= (1 << PB7);
    // Set pin to a logic high
    PORTB |= (1 << PB7);
        
    while(1) {
            
        // Blink led every second
        _delay_ms(500);
        PORTB &=  ~(1 << PB7);
        _delay_ms(500);
        PORTB |= (1 << PB7);
            
    }
}

